#version 450

layout(location = 0) in vec2 vUV;
layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 0) uniform sampler2D uTex;

// Push constants: window center/width (normalized), inversion, and view params
layout(push_constant) uniform Params {
    float winCenter;  // normalized [0,1]
    float winWidth;   // normalized > 0
    int   invert;     // 0 or 1
    float zoom;       // > 0
    float offsetX;    // pan in UV space
    float offsetY;    // pan in UV space
} params;

void main() {
    // If outside texture UV range, show black (letterbox) rather than clamped edge
    if (vUV.x < 0.0 || vUV.x > 1.0 || vUV.y < 0.0 || vUV.y > 1.0) {
        outColor = vec4(0.0, 0.0, 0.0, 1.0);
        return;
    }
    float s = texture(uTex, vUV).r; // R16_UNORM -> normalized [0,1]
    float ww = max(params.winWidth, 1e-6);
    // DICOM windowing in normalized domain: v = clamp((s - WC)/WW + 0.5, 0, 1)
    float v = clamp((s - params.winCenter) / ww + 0.5, 0.0, 1.0);
    if (params.invert != 0) v = 1.0 - v;
    outColor = vec4(v, v, v, 1.0);
}